/***********************
 * 参数配置页面逻辑代码
 ***********************/
$(function(){
    function config(){
        function init()
        {
            $('.config-form input').toArray().forEach(element => {
                var key = element.id.replace('input_','');
                $.get(`${Config.BASE_URL}/api/configuration/${key}`,function(response){
                    if (response.succeeded && response.data) {
                        $(element).val(response.data.value);
                    }
                });
            });
        }

        function initUI(){
            $('.config-form input').on('change',function(){
                var key = this.id.replace('input_','');
                var data = {
                    name: $(this).attr('name'),
                    key:key,
                    value: $(this).val()
                };
                $.post(`${Config.BASE_URL}/api/configuration/or-update`,JSON.stringify(data),function(response){
                    if (response.succeeded) {
                        $.notify('修改成功！', { type: 'success' });
                    }
                    else
                    {
                        $.notifyErrors(response.errors);
                    }
                });
            });
        }

        return{
            init:init,
            initUI:initUI
        }
    }

    window.SysConfig = config();
    SysConfig.initUI();
})